<?php
/*
nueva libreria para mostrar los links del ADMINISTRADOR: adicionar, editar y borrar. Se crea para personalizar la visualización de los lins de acuerdo al tipo de contest: Internal, External y Postmaraton
*/
//require_once(LIBDIR . '/lib_db2.php'); //lib encargada conexion con db

//global $db;
//$db = db2();
$db = "domjudge2";

//funcion para verificar si el contest es tipo postmaraton,la consulta se hace en db2
function type_contest($table, $cid, $conexion)
{
	//global $DB;
	global $db;
	if( $table == "contest" )
	{
		//$pos = $DB->q('VALUE SELECT post FROM contest WHERE cid = %i ',$cid);
		$respuesta = array();
		$query = "SELECT `inner`,`post` FROM `contest` WHERE `cid`='$cid'";
		$res = mysql_query($query ,$conexion);
		mysql_select_db($db , $conexion);
		if($res){
			$contest = mysql_fetch_array($res, MYSQL_BOTH);
			/*if($contest['inner'] == '0' AND $contest['post'] == '0')
			{
				$respuesta[0] = "External Contest";
				$respuesta[1] = "contests";//name file to return when delete was calceled
			}*/

			if($contest['inner'] == '0' AND $contest['post'] == '1')
			{
				$respuesta[0] = "Postmaraton";
				$respuesta[1] = "postmaratones";
			}elseif($contest['inner'] == '1' AND $contest['post'] == '0')
			{
				$respuesta[0] = "Internal Contest";
				$respuesta[1] = "inner_contests";
			}else{
				//si no se trata de alguna de las anteriores opciones, se trata de un External
				//Contest, entonces no se busca en db2 porque se almacena en db1
				//simplemente se retorna el nombre de la tabla y pagina
				$respuesta[0] = "External Contest";
				$respuesta[1] = "contests";//name file to return when delete was calceled
			}
			return $respuesta;
		}else{
			throw new RuntimeException("SQL syntax-error, ". "Error " .
				mysql_error($conexion) . ", query: '$query'");
		}
	}
}

function add_link($tab, $multi = false)
{
	if( $tab == "External Contest" )	
		$table = "contest";

	if($tab == "Internal Contest")
		$table = "inner_contest";
		
	
	return "<a href=\"" . htmlspecialchars($table) . ".php?cmd=add\">" .
		"<img src=\"../images/add" . ($multi?"-multi":"") .
		".png\" alt=\"add" . ($multi?" multiple":"") .
		"\" title=\"add" .   ($multi?" multiple":"") .
		" new " . htmlspecialchars($tab) . "\" class=\"picto\" /></a>";
}

function edit_link($tab, $value, $multi = false)
{
	if( $tab == "External Contest" )	
		$table = "contest";

	if($tab == "Internal Contest")
		$table = "inner_contest";
		
	return "<a href=\"" . htmlspecialchars($table) . ".php?cmd=edit" .
		($multi ? "" : "&amp;id=" . urlencode($value) ) .
		"&amp;referrer=" . urlencode(basename($_SERVER['SCRIPT_NAME']) .
		(empty($_REQUEST['id']) ? '' : '?id=' . urlencode($_REQUEST['id']))) .
		"\">" .
		"<img src=\"../images/edit" . ($multi?"-multi":"") .
		".png\" alt=\"edit" . ($multi?" multiple":"") .
		"\" title=\"edit " .   ($multi?"multiple ":"this ") .
		htmlspecialchars($tab) . "\" class=\"picto\" /></a>";
}


function del_link($table, $field, $value)
{
	if( $table == "postmaraton" ) 
	{
		$tab = "postmaraton";
		$table = "contest";
	}

	if($table == "External Contest" )
	{
		$tab = "External Contest";
		$table = "contest";
	}

	if( $table == "Internal Contest" )
	{
		$tab = "Internal Contest";
		$table = "contest";
	}
	return "<a href=\"delete.php?table=" .urlencode($table)  . "&amp;" .
		$field . "=" . urlencode($value) ."\"><img src=\"../images/delete.png\" " .
		"alt=\"delete\" title=\"delete this ".urlencode($tab).
		"\" class=\"picto\" /></a>";
}

?>
